package com.qczl.pme.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.qczl.pme.service.StbService;
import com.qczl.pme.vo.StbVo;
import com.qczl.util.SpringUtil;

public class StbFilter implements HandlerInterceptor {
	public static boolean check(String str, String strs[]) {
		for (String s : strs) {
			if (str.contains(s)) {
				return true;
			}
		}
		return false;
	}

	Logger log = Logger.getLogger(this.getClass());
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String url = request.getRequestURI();
		String [] urls = {"/stb/"};//拦截的路径
		if(check(url, urls)&&request.getParameter("skip")==null){
			String stbno = request.getParameter("stbno");
			StbService service =  (StbService)SpringUtil.getBean("stbServiceImpl");
			StbVo vo = service.getByStbNo(stbno);
			log.info("拦截了");
			if(vo!=null){
				return true;
			}else{
				log.info("拦截了:"+url+"机顶盒不可用");
				response.sendRedirect("/golauncher?msg=该机顶盒暂不可用");
				return false;
			}
		}else{
			log.info("没拦截"+url);
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
